const queryString =  (arr) => {
    let result = ''
    for(let key in arr) {
        if(typeof arr[key] == 'object'){
            let str = ''
            arr[key].forEach(item => {
                str += key + '=' + item + '&'
            })
            result += str
            
        }
    }
    return result
}

const param = (arr) => {
    return new Promise(resolve => {
        let obj = {}
            for(let key in arr) {
                let temp = arr[key] instanceof Array
                if(!temp) {
                   obj[key] = arr[key]
                   resolve(obj)
                   
                }
            }
    })
}
// 存储数据信息
export const state = () => {
    return {
        indexTop:{},
        indexBtm:{},
        loading:true
    }
}

export const mutations = {
    'SET_indexTop'(state, data) {
        state.indexTop = data
        state.loading = true
       
    },
    'SET_indexBtm'(state, data) {
        state.indexBtm = data
        state.loading = true
    },
    'LOADING'(state,bool) {
        state.loading = bool
    }
}

export const actions = {
    'FILTER_DATA_TOP'({state,commit}, indexTop) {
        commit('SET_indexTop',indexTop)
        return this.$axios({
            url:'/hotels',
            params:{
                ...state.indexTop,
                ...state.indexBtm
            }
        }).then(res => {
            if(res.status == 200) {
                commit('LOADING',false)
            }
            return res
        })
    },
    async 'FILTER_DATA_BTM'({state,commit}, indexTop) {
        const res = await param(indexTop)
        
        commit('SET_indexBtm',res)
        const result = queryString(indexTop)
        return this.$axios({
            url:'/hotels?' + result,
            params:{
                ...state.indexBtm,
                ...state.indexTop
            }
           
        }).then(res => {
            if(res.status == 200) {
               commit('LOADING',false)
            }
            res.que = result
            return res
        })
    },
}


